/**
 * Created by mapbar_front on 2019/8/26.
 */
import React,{Component} from 'react';
import {Button} from 'antd';



class ShowNum extends Component{
    constructor(props){
        super(props);
        this.state = {
            num: 123,
        }
    }
    static getDerivedStateFormProps(nextProps,prevState){
        console.log('***getDerivedStateFormProps***');
        if(nextProps.num !== prevState.num){
            console.log('nextProps.num',nextProps.num);
            console.log('prevState.num',prevState.num);
            return {
                num:nextProps.num
            }
        }
        return null;
    }
    // componentWillReceiveProps(nextProps){
    //     console.log('---receive props---');
    //     console.log('this.props',this.props);
    //     console.log('nextProps',nextProps);
    // }
    shouldComponentUpdate(newProps){
        console.log('新的newProps');
        if(newProps.num !== this.props.num){
            console.log('newProps.num',newProps.num);
            console.log('this.props.num',this.props.num);
            return true;
        }else {
            console.log('else');
            console.log('newProps.num',newProps.num);
            console.log('this.props.num',this.props.num);
        }
        return false;
    }
    componentDidUpdate(prevProps,prevState){
       console.log('---componentDidUpdate2---');
       console.log('prevProps',prevProps);
       console.log('prevState',prevState);
    }
    render(){
        return(
            <div>
                {this.props.num}
            </div>
        )
    }
}
class LifeCycle extends Component{
    constructor(props){
        super(props);
        this.state = {
            num:0,
        }
    }
    getSnapshotBeforeUpdate(nextProps,prevState){
        console.log('***getSnapshotBeforeUpdate***');
    }
    // componentWillMount(){
    //     console.log('will mount');
    // }
    componentDidMount(){
        console.log('did mount');
    }
    shouldComponentUpdate(newProps,newState){
        console.log('---should update---');
        console.log('newProps',newProps);
        console.log('newState',newState);
        return true;
    }
    // componentWillUpdate(nextProps,nextState){
    //     console.log('---will update---');
    // }
    componentDidUpdate(prevProps,prevState){
        console.log('---did update---');
        console.log('prevProps',prevProps);
        console.log('prevState',prevState);
    }
    addNum=()=>{
        this.setState({
            num: this.state.num+1
        })
    }
    render(){
        return(
            <div>
                <Button onClick={this.addNum}>
                    LifeCycle
                </Button>
                <ShowNum num={this.state.num}/>
            </div>
        )
    }
}
export default LifeCycle;

